import util.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by loso on 2017/3/9.
 */
public class _501_FindModeInBinarySearchTree {

    Map<Integer,Integer> map = new HashMap<>();
    public int[] findMode(TreeNode root) {
        List<Integer> out = new ArrayList<>();
        check(root);
        int max = 0;
        for (int a : map.values()){
            max = max>a ?max:a;
        }
        for (int a : map.keySet()){
            if (map.get(a) == max)
                out.add(a);
        }
        int[] outs = new int[out.size()];
        for (int i = 0; i < out.size(); i++) {
            outs[i] = out.get(i);
        }
        return outs;

    }

    public void check(TreeNode root){
        if (root == null){
            return;
        }
        map.put(root.val,map.getOrDefault(root.val,0)+1);
        check(root.left);
        check(root.right);
    }
}
